package cn.mesmile.storage.util;

import cn.mesmile.storage.entity.ProgressInputStream;
import cn.mesmile.storage.entity.UploadListener;
import io.minio.MinioClient;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.MinioException;
import lombok.SneakyThrows;
import org.xmlpull.v1.XmlPullParserException;

import javax.swing.*;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.rmi.ServerException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author zb
 * @date 2021/3/12 17:43
 * @Description  minio 上传进度条  https://blog.csdn.net/xu912996973/article/details/115073380
 */
public class MinioUtils {

      /*
        InvalidBucketNameException : 非法的存储桶名称。
        NoResponseException : 服务器无响应。
        IOException : 连接异常
        org.xmlpull.v1.XmlPullParserException : 解析返回的XML异常
        ErrorResponseException : 执行失败
        InternalException : 内部异常
      */

    public static void main(String[] args) throws NoSuchAlgorithmException, IOException, InvalidKeyException, XmlPullParserException {
        try {
            // 使用MinIO服务的URL，端口，Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = new MinioClient("https://play.min.io",
                    "Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
            // 检查存储桶是否已经存在
            boolean isExist = minioClient.bucketExists("abimages");
            if(isExist) {
                System.out.println("Bucket already exists.");
            } else {
                // 创建一个名为asiatrip的存储桶，用于存储照片的zip文件。
                minioClient.makeBucket("abimages");
            }
            File file = new File("D:\\图片压缩利器Imagine.rar");
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
            ProgressInputStream progressInputStream = new ProgressInputStream(bufferedInputStream, new UploadListener() {
                @Override
                public void onProgress(int process) {
                    // 回调方法返回 进度
                    System.out.println("------------  ");
                    System.out.println("process = " + process);
                    System.out.println("------------  ");
                }
            });

            minioClient.putObject("abimages","图片压缩利器Imagine.rar",progressInputStream, progressInputStream.available(),"image/jpg");
            System.out.println("my-objectname is uploaded successfully");
            // 使用putObject上传一个文件到存储桶中。
//            minioClient.putObject("abimages","ex1.png", "E:\\images\\ex1.png");
            System.out.println("/home/user/Photos/asiaphotos.zip is successfully uploaded as asiaphotos.zip to `asiatrip` bucket.");
            System.out.println("https://play.min.io/abimages/图片压缩利器Imagine.rar");
        } catch(MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }


    /*
     *  File file = new File("E:\\images\\ex2.png");
     *             FileInputStream fileInputStream = new FileInputStream(file);
     *             BufferedInputStream bis = new BufferedInputStream(fileInputStream);
     *
     *             ProgressMonitorInputStream df = new ProgressMonitorInputStream(null,
     *                     "Reading  " + file.getName(), bis);
     *             int available = df.available();
     *             byte [] b = new byte[1024];
     *             if (df.read(b) != -1){
     *                 String s = new String(b);
     *                 System.out.println(">>>>>>>>>>>>  s = " + s);
     *             }
     */

}
